
import { _decorator, Component, Node, assetManager, assert, Asset, JsonAsset, find, MeshRenderer, Texture2D, ImageAsset, instantiate, Prefab } from 'cc';
const { ccclass, property } = _decorator;

import jszip from "jszip/dist/jszip.min.js";

@ccclass('Npmjszip')
export class Npmjszip extends Component {

    testBundle: import("cc").AssetManager.Bundle;

    onLoad() {

        assetManager.loadBundle('asset_bundle', () => { //先加载bundle文件夹



            this.testBundle = assetManager.getBundle('asset_bundle');//加载完成后，获取到指定文件夹

            console.log(this.testBundle);

            this.zip_img(); //加载 zip 图片
            this.zip_model(); //提加载 zip 预制体
        });
    }
    start() {

        // [3]


    }


    //图片解压，加载
    zip_model() {

        let self = this;

        let Res_dir = "zip_model";//文件夹

        //data.nativeUrl 的直接获取
        var infos = [];
        this.testBundle.getDirWithPath(Res_dir, Asset, infos);//getDirWithPath 获取在某个指定文件夹下的所有资源信息
        // console.log(this.testBundle.getAssetInfo("zip_img/ditu_zip"));
        // console.log(this.testBundle.get("ditu_zip"));

        this.testBundle.load("zip_model/Cube", Prefab, (finished: number, total: number, item: any) => {
            //加载过程          
            var nmb = ((finished / total) * 100.00).toFixed(2) + '%';
            console.log(nmb);
        }, (err: Error | null, data) => {

            let Model = find("Model");
            let p_ = instantiate(data) as Node;
            p_.parent = Model;

        }
        )

        //.zip 文件
        this.testBundle.load(Res_dir + "/" + "model_zip", Asset, (finished: number, total: number, item: any) => {
            //加载过程          
            var nmb = ((finished / total) * 100.00).toFixed(2) + '%';
            console.log(nmb);
        }, (err: Error | null, data: any) => {//data.nativeUrl
            //得到地址后，再进行load，需要写.bin 扩展
            // assetManager.loadAny({ url: data.nativeUrl, ext: ".bin" }, (err, file) => {
            //     if (err) {
            //         console.error("readFromBinaryFile Error: " + data.nativeUrl + err.message);
            //     }
            //     try {

            //         jszip.loadAsync(file).then((zip) => {

            //             console.log(file);

            //             zip.file("Cube.prefab").async("string").then((data) => {
            //                 console.log(data);

            //                 // let Model = find("Model");
            //                 // let p_ = instantiate(data) as Node;//--------这种还，不行
            //                 // p_.parent = Model;

            //             })


            //         })

            //     } catch (e) {
            //         throw new Error(e);
            //     }
            // });

        }
        )
    }



    //图片解压，加载
    zip_img() {

        let self = this;

        let Res_dir = "zip_img";//文件夹

        //data.nativeUrl 的直接获取
        // var infos = [];
        // this.testBundle.getDirWithPath(Res_dir, Asset, infos);//getDirWithPath 获取在某个指定文件夹下的所有资源信息
        // console.log(this.testBundle.getAssetInfo("zip_img/ditu_zip"));
        // console.log(this.testBundle.get("ditu_zip"));


        //ditu_zip:图片的压缩文件，需要是.zip 文件
        this.testBundle.load(Res_dir + "/" + "ditu_zip", Asset, (finished: number, total: number, item: any) => {

            //加载过程          
            var nmb = ((finished / total) * 100.00).toFixed(2) + '%';
            console.log(nmb);

        }, (err: Error | null, data: any) => {//data.nativeUrl

            //得到地址后，再进行load，需要写.bin 扩展
            assetManager.loadAny({ url: data.nativeUrl, ext: ".bin" }, (err, file) => {
                if (err) {
                    console.error("readFromBinaryFile Error: " + data.nativeUrl + err.message);
                }
                try {

                    jszip.loadAsync(file).then((zip) => {

                        zip.file("ditu.jpg").async("base64").then((data) => {
                            // console.log("data:image/jpg;base64," + data);

                            var img = new Image();
                            var base = "data:image/jpg;base64," + data;
                            img.src = base;
                            img.onload = function () {
                                let imgAsset = new ImageAsset();//重置此图像资源使用的原始图像源
                                imgAsset.reset(img);

                                let texture = new Texture2D();
                                texture.image = imgAsset;


                                let dimiancc = find("Plane");
                                var com = dimiancc.getComponent(MeshRenderer);
                                com.material.setProperty('mainTexture', texture);//材质赋值

                            }


                        })


                    })

                } catch (e) {
                    throw new Error(e);
                }
            });

        }
        )
    }

}
